package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Coupon;
import com.hboxs.asl.entity.CouponCode;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.CouponCodeService;
import com.hboxs.asl.service.CouponService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xxx on 2016/12/10
 * 优惠卷接口
 */
@Controller(value = "appMemberCouponController")
@RequestMapping(value = "/app/m/coupon")
public class CouponController extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "couponCodeServiceImpl")
    private CouponCodeService couponCodeService;
    @Resource(name = "couponServiceImpl")
    private CouponService couponService;
    @Resource(name = "messageServiceImpl")
    private MessageService messageService;

    /**
     * 领取优惠卷
     *
     * @param id 购物卷id
     * @return json
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public
    @ResponseBody
    Object get(Long id) {

        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }

            Coupon coupon = couponService.find(id);
            if (coupon == null) {
                return JsonMessage.error("优惠券不存在");
            }
            Long num = couponService.findReceivableCount4Coupon(member, coupon);
            if (num > 0) {
                couponCodeService.build(coupon, member);
            }
            if (num - 1 > 0) {
                messageService.addMessage(memberService.getCurrentByToken(), "领取成功", "恭喜成功领取优惠券【"+coupon.getName()+"】");
                return JsonMessage.success("领取成功，您还有 " + (num - 1) + " 次领取机会");
            } else {
                return JsonMessage.success("已无领取次数");
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取优惠卷列表
     *
     * @param status 筛选条件;0:未使用;1:已使用;2已过期;
     * @return 优惠卷列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    Object list(Integer status) {

        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if (status == null) {
                return JsonMessage.error("筛选条件不能为空");
            }
            List<CouponCode> couponCodes = new ArrayList<>();
            switch (status) {
                case 0:
                    couponCodes = couponCodeService.findUnusedCouponCodes(member);
                    break;
                case 1:
                    couponCodes = couponCodeService.findUsedCouponCodes(member);
                    break;
                case 2:
                    couponCodes = couponCodeService.findOverdueCouponCodes(member);
                    break;
                default:
                    break;
            }

            return JsonMessage.success(
                    JsonMessage.optimiseObj(
                            couponCodes, "coupon.cut", "coupon.name", "coupon.maximumPrice", "code", "coupon.endDate"
                    )
            );

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


}
